package Test;

import DB.SemesterDB;
import DB.TeamDB;
import Model.Semester;
import Model.Team;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.fail;

public class TeamDBTest {

    //Ok
    @Test
    public void testInsertTeam() throws Exception {
        TeamDB teamDB = new TeamDB();
        SemesterDB semesterDB = new SemesterDB();
        Semester semester = semesterDB.findSemester(1);
        int rc = teamDB.insertTeam(new Team(teamDB.teamGetMax(), "DMAA0913", "2013-01-01", "2013-01-01", semester));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting team successful");
        }
        else {
            fail("Failed inserting team");
        }
    }

    //Ok
    @Test
    public void testFindTeam() throws Exception {
        TeamDB teamDB = new TeamDB();
        Team team = teamDB.findTeam(1);

        if (team != null) {
            System.out.println(team.getId());
            System.out.println(team.getName());
            System.out.println(team.getStartDate());
            System.out.println(team.getEndDate());
            System.out.println(team.getSemester().getSemesterNo());
        }
        else {
            fail("Failed finding a team - team is null");
        }
    }

    //Ok
    @Test
    public void testGetAllTeams() throws Exception {
        TeamDB teamDB = new TeamDB();
        ArrayList<Team> teamObj = teamDB.getAllTeams(false);

        if (teamObj != null) {
            for (Team t : teamObj) {
                System.out.println(t.getName());
            }
        }
        else {
            fail("No teams found");
        }
    }

    //Ok
    @Test
    public void testUpdateTeam() throws Exception {
        TeamDB teamDB = new TeamDB();
        String updateValue = "DMAA0213";

        Team team = new Team(1, updateValue, "2013-09-02", "2013-12-20", new Semester(1, 2,  "2013-09-02", "2013-12-20"));

        teamDB.updateTeam(team);

        String teamName = teamDB.findTeam(1).getName();

        if (teamName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }

    }

    //Ok
    @Test
    public void testDeleteTeam() throws Exception {
        TeamDB teamDB = new TeamDB();
        Team team = new Team(1000, "Delete", "2013-01-01", "2013-01-01", new Semester(1, 2,  "2013-09-02", "2013-12-20"));
        teamDB.insertTeam(team);

        if (teamDB.findTeam(1000) != null) {
            teamDB.deleteTeam(1000);

            if (teamDB.findTeam(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Insert for deletion failed - team is null");
        }
    }
}
